package Tree;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.time.LocalTime;
import java.util.*;
import java.util.List;
import java.util.Timer;

public class InteractiveDeskPet {
    private static Map<String, String> dailyTasks = new LinkedHashMap<>();
    private static List<String> completedTasks = new ArrayList<>();
    private static String currentTask = "暂无任务";
    private static String nextTaskTime = "无";

    public static void main(String[] args) {
        // 初始化任务
        initializeTasks();

        // 创建桌宠
        createDeskPet();

        // 定时更新任务
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                updateCurrentTask();
            }
        }, 0, 60 * 1000); // 每分钟更新一次
    }

    // 初始化每日任务
    private static void initializeTasks() {
        dailyTasks.put("07:00", "学习Java网课（1小时）。");
        dailyTasks.put("12:20", "学习口号30分钟，激励自己！");
        dailyTasks.put("16:25", "跑步1小时，提升多巴胺水平。");
        dailyTasks.put("18:00", "复习Java多线程（2小时）。");
        dailyTasks.put("21:00", "算法练习（2小时）。");
    }

    // 创建桌宠窗口
    private static void createDeskPet() {
        JFrame petFrame = new JFrame();
        petFrame.setUndecorated(true); // 无边框
        petFrame.setSize(200, 150);
        petFrame.setAlwaysOnTop(true); // 保持窗口在最前方
        petFrame.setLayout(new BorderLayout());
        petFrame.setBackground(new Color(0, 0, 0, 0)); // 透明背景

        // 添加桌宠区域
        JPanel petPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 绘制桌宠
                g2d.setColor(Color.PINK);
                g2d.fillOval(50, 20, 100, 100); // 头部
                g2d.setColor(Color.BLACK);
                g2d.fillOval(80, 50, 10, 10); // 左眼
                g2d.fillOval(110, 50, 10, 10); // 右眼
                g2d.drawArc(80, 70, 40, 20, 0, -180); // 嘴巴
            }
        };

        petPanel.setOpaque(false);
        petPanel.setPreferredSize(new Dimension(200, 100));
        petFrame.add(petPanel, BorderLayout.CENTER);

        // 添加任务显示
        JLabel taskLabel = new JLabel("<html><center>当前任务：<br>" + currentTask + "</center></html>", SwingConstants.CENTER);
        taskLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        petFrame.add(taskLabel, BorderLayout.SOUTH);

        // 设置窗口拖动
        petFrame.addMouseListener(new MouseAdapter() {
            private int xOffset, yOffset;

            @Override
            public void mousePressed(MouseEvent e) {
                xOffset = e.getX();
                yOffset = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                xOffset = 0;
                yOffset = 0;
            }
        });

        petFrame.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int x = e.getXOnScreen() - petFrame.getWidth() / 2;
                int y = e.getYOnScreen() - petFrame.getHeight() / 2;
                petFrame.setLocation(x, y);
            }
        });

        // 添加交互功能
        petPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) {
                    if (!currentTask.equals("暂无任务")) {
                        completedTasks.add(currentTask);
                        JOptionPane.showMessageDialog(petFrame, "任务完成！真棒！", "鼓励", JOptionPane.INFORMATION_MESSAGE);
                        dailyTasks.remove(nextTaskTime);
                        updateCurrentTask();
                    } else {
                        JOptionPane.showMessageDialog(petFrame, "没有任务啦！放松一下吧！", "提示", JOptionPane.INFORMATION_MESSAGE);
                    }
                } else if (SwingUtilities.isRightMouseButton(e)) {
                    JOptionPane.showMessageDialog(petFrame, "加油哦！下一个任务是：" + currentTask, "鼓励", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        // 设置初始位置和显示
        petFrame.setLocation(100, 100); // 初始位置
        petFrame.setVisible(true);
    }

    // 更新当前任务
    private static void updateCurrentTask() {
        LocalTime now = LocalTime.now();
        nextTaskTime = "无";
        currentTask = "暂无任务";

        for (Map.Entry<String, String> entry : dailyTasks.entrySet()) {
            LocalTime taskTime = LocalTime.parse(entry.getKey());
            if (taskTime.isAfter(now)) {
                nextTaskTime = entry.getKey();
                currentTask = entry.getValue();
                break;
            }
        }
    }
}
